package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    //分页查询订单详情
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return orderService.findOrderInfosByConditions(queryPageBean);
    }

    //电话预约
    @RequestMapping("/orderByCall")
    public Result orderByCall(@RequestBody Map map,String[] setmealIds){
       try {
            return orderService.orderByCall(map, setmealIds);
       }catch (Exception e){
           e.printStackTrace();
           return null;
       }
    }

    //订单信息编辑回显
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //订单信息编辑提交
    @RequestMapping("/update")
    public Result update(@RequestBody Map<String,Object> map,int id){
        try {
            return orderService.update(map,id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"更新预约订单失败");
        }
    }

    //删除订单
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            orderService.delete(id);
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    //开关修改预约状态
    @RequestMapping("/changeOrderStatus")
    public Result changeOrderStatus(@RequestBody Map<String,Object> map){
        try {
            orderService.changeOrderStatus(map);
            return new Result(true,MessageConstant.UPDATE_ORDERSTATUS_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.UPDATE_ORDERSTATUS_FAIL);
        }
    }

}
